//
//  StatusOriginalView.m
//  Weibo
//
//  Created by Anasue on 9/16/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "StatusOriginalView.h"
#import "StatusOriginalViewFrame.h"
#import "Status.h"
#import "User.h"
#import "UIImageView+WebCache.h"
#import "StatusPhotosView.h"
#import "StatusLabel.h"

@interface StatusOriginalView ()
/** 昵称 */
@property (nonatomic, weak) UILabel *nameLabel;
/** 正文 */
@property (nonatomic, weak) UILabel *textLabel;
/** 来源 */
@property (nonatomic, weak) UILabel *sourceLabel;
/** 时间 */
@property (nonatomic, weak) UILabel *timeLabel;
/** 头像 */
@property (nonatomic, weak) UIImageView *iconView;
/** 配图 */
@property (nonatomic, weak) StatusPhotosView *photosView;
/** vip */
@property (nonatomic, weak) UIImageView *vipView;
@end

@implementation StatusOriginalView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        
        //创建昵称
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.font = StatusOriginalNameFont;
//        nameLabel.textColor = [UIColor orangeColor];
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        //创建正文
        StatusLabel *textLabel = [[StatusLabel alloc]init];
//        textLabel.numberOfLines = 0;
//        textLabel.font = StatusOriginalTextFont;
        [self addSubview:textLabel];
        self.textLabel = textLabel;
        
        //创建时间
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.font = StatusOriginalTimeFont;
        timeLabel.textColor = [UIColor orangeColor];
        [self addSubview:timeLabel];
        self.timeLabel = timeLabel;

        //创建来源
        UILabel *sourceLabel = [[UILabel alloc]init];
        sourceLabel.font = StatusOriginalSourceFont;
        [self addSubview:sourceLabel];
        self.sourceLabel = sourceLabel;

        //创建头像
        UIImageView *iconView = [[UIImageView alloc]init];
        [self addSubview:iconView];
        self.iconView = iconView;
        
        //创建配图
        StatusPhotosView *photosView = [[StatusPhotosView alloc]init];
        [self addSubview:photosView];
        self.photosView = photosView;
        
        //创建vip图标
        UIImageView *vipView = [[UIImageView alloc]init];
        [self addSubview:vipView];
        self.vipView = vipView;

    }
    return self;
}

- (void)setOriginalViewFrame:(StatusOriginalViewFrame *)originalViewFrame
{
    _originalViewFrame = originalViewFrame;
    self.frame = originalViewFrame.frame;
    
    Status *status = originalViewFrame.status;
    User *user = status.user;
    
    //设置子控件的frame及内容
    self.nameLabel.text = user.name;
    self.nameLabel.frame = originalViewFrame.nameFrame;
    if (status.user.isVip) {
        NSString *level = [NSString stringWithFormat:@"common_icon_membership_level%d",user.mbrank];
        self.vipView.image = [UIImage imageWithName:level];
        self.vipView.frame = originalViewFrame.vipFrame;
        self.nameLabel.textColor = [UIColor orangeColor];
        self.vipView.hidden = NO;
    }else{
        self.nameLabel.textColor = [UIColor blackColor];
//        self.vipView.hidden = YES;
    }
    
    //时间
    NSString *time = status.created_at;
    self.timeLabel.text = time;
    CGFloat timeX = CGRectGetMinX(self.nameLabel.frame);
    CGFloat timeY = CGRectGetMaxY(self.nameLabel.frame) + StatusCellInSet * 0.5;
    NSMutableDictionary *timeAttr = [NSMutableDictionary dictionary];
    timeAttr[NSFontAttributeName] =StatusOriginalTimeFont;
    CGSize timeSize = [time sizeWithAttributes:timeAttr];
    self.timeLabel.frame = (CGRect){{timeX,timeY},timeSize};
    
    //来源
    self.sourceLabel.text = status.source;
    CGFloat sourceX = CGRectGetMaxX(self.timeLabel.frame) + StatusCellInSet * 0.5;
    CGFloat sourceY = timeY;
    NSMutableDictionary *sourceAttr = [NSMutableDictionary dictionary];
    sourceAttr[NSFontAttributeName] =StatusOriginalSourceFont;
    CGSize sourceSize = [status.source sizeWithAttributes:sourceAttr];
    self.sourceLabel.frame = (CGRect){{sourceX,sourceY},sourceSize};

    //正文
//    self.textLabel.text = status.text;
    self.textLabel.attributedText = status.attributedText;
    self.textLabel.frame = originalViewFrame.textFrame;
    //头像
    [self.iconView setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageWithName:@"avatar_default_small"]];
    self.iconView.frame = originalViewFrame.iconFrame;
    //裴团
    if (status.pic_urls.count) { // 有配图
        self.photosView.frame = originalViewFrame.photosFrame;
        self.photosView.pic_urls = status.pic_urls;
        self.photosView.hidden = NO;
    } else {
        self.photosView.hidden = YES;
    }
        
}




@end























